package com.ogawa.projectcommon.widget

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import com.ogawa.projectcommon.R

import com.ogawa.projectcommon.activity.BaseMqttActivity
import com.ogawa.projectcommon.datacollect.DataCollectUtils
import kotlinx.android.synthetic.main.layout_lev_5d.view.*

class LevelView5D : FrameLayout, View.OnClickListener {
    private var lev = 0
    private var func: String = ""
    private var modelValue: String = ""
    private lateinit var activity: BaseMqttActivity
    private var publishEnable = true
    private var deviceType = ""
    private var type = 0
    private var programName = ""

    constructor(context: Context) : super(context)

    @SuppressLint("CustomViewStyleable")
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        LayoutInflater.from(context).inflate(R.layout.layout_lev_5d, this)
        tv_1.setOnClickListener(this)
        tv_2.setOnClickListener(this)
        tv_3.setOnClickListener(this)
        tv_4.setOnClickListener(this)
        tv_5.setOnClickListener(this)
        tv_6.setOnClickListener(this)
        val a = context.obtainStyledAttributes(attrs, R.styleable.LevelViewAttr)
        tv_lev_text.text = a.getString(R.styleable.LevelViewAttr_title)
        publishEnable = a.getBoolean(R.styleable.LevelViewAttr_publishEnable, true)
        a.recycle()
        if (context is BaseMqttActivity) {
            activity = context
        }
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    fun setFunc(func: String, modelValue: String = "", deviceType: String? = null) {
        this.func = func
        this.modelValue = modelValue
        if (deviceType != null) {
            this.deviceType = deviceType
        }
    }

    @SuppressLint("SetTextI18n")
    fun setLev(lev: Int) {
        this.lev = lev
        tv_1.isSelected = lev == 1
        tv_2.isSelected = lev == 2
        tv_3.isSelected = lev == 3
        tv_4.isSelected = lev == 4
        tv_5.isSelected = lev == 5
        tv_6.isSelected = lev == 6
        if (lev > 0) {
            tv_lev.text = lev.toString() + context.getString(R.string.lev)
        }
        if (lev == 0) {
            if (publishEnable) {
                tv_lev.text = context.getString(R.string.unadjustable)
            } else {
                tv_lev.text = ""
            }
        }
        if (lev == -1) {
            tv_lev.text = ""
        }
    }

    fun setTitle(title: String) {
        tv_lev_text.text = title
    }

    override fun onClick(v: View?) {
        if (publishEnable) {
            if (!activity.isSnExist()) {
                return
            }
            if (func.isEmpty() || lev == 0 || lev == -1) {
                return
            }
            if (modelValue.isEmpty()) {
                modelValue = "10"
            }
            var value = 0
            when (v?.id) {
                R.id.tv_1 -> {
                    activity.publish(func, modelValue, "1")
                    value = 1
                }
                R.id.tv_2 -> {
                    activity.publish(func, modelValue, "2")
                    value = 2
                }
                R.id.tv_3 -> {
                    activity.publish(func, modelValue, "3")
                    value = 3
                }
                R.id.tv_4 -> {
                    activity.publish(func, modelValue, "4")
                    value = 4
                }
                R.id.tv_5 -> {
                    activity.publish(func, modelValue, "5")
                    value = 5
                }
                R.id.tv_6 -> {
                    activity.publish(func, modelValue, "6")
                    value = 6
                }
            }
            updateLevChange(value)
        } else {
            when (v?.id) {
                R.id.tv_1 -> setLev(1)
                R.id.tv_2 -> setLev(2)
                R.id.tv_3 -> setLev(3)
                R.id.tv_4 -> setLev(4)
                R.id.tv_5 -> setLev(5)
                R.id.tv_6 -> setLev(6)
            }
        }
    }

    fun setProgramInfo(type: Int, programName: String) {
        this.type = type
        this.programName = programName
    }

    private fun updateLevChange(value: Int) {
        DataCollectUtils.strengthEvent(value, type, programName, "$func,$modelValue", deviceType)
    }

    fun getLev(): Int {
        return lev
    }
}